<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>yaml备份</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <blockquote class="layui-elem-quote layui-text">
            说明:<br>
        </blockquote>

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">资源名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="res_name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline" id="timeSelect">
                            <label class="layui-form-label">时间范围</label>
                            <div class="layui-input-inline" style="width:200px">
                              <input type="txt" id="starttime" name="starttime" value="" class="layui-input textboxtime" placeholder="开始时间" />
                            </div>
                            <div class="layui-form-mid">-</div>
                            <div class="layui-input-inline" style="width:200px">
                              <input type="txt" id="endtime" name="endtime" value="" class="layui-input textboxtime" placeholder="结束时间" />
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="createBackup">创建备份</button>
        </script>

        <table class="layui-table" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="viewYaml">查看</a>
            <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="restoreBackup">恢复</a>
        </script>

    </div>
</div>
<script type="text/html" id="statusTpl">
  {{# if (d.resType == 'deployment') { }}
     <span style="color:#218868">{{d.resType}}</span>
	{{# } else if (d.resType == 'configmap') { }}
    <span style="color:#1E9FFF">{{d.resType}}</span>
{{# }  else if (d.resType == 'secret') { }}
    <span style="color:#5FB878">{{d.resType}}</span>
{{# }  else if (d.resType == 'ingress') { }}
    <span style="color:#2F4056">{{d.resType}}</span>
{{# }  else if (d.resType == 'service') { }}
    <span style="color:#FF5722">{{d.resType}}</span>
{{# }  else if (d.resType == 'hpa') { }}
    <span style="color:#FFB800">{{d.resType}}</span>
{{# }  else if (d.resType == 'cronjob') { }}
    <span style="color:#57f1c3">{{d.resType}}</span>
{{# }  else if (d.resType == 'statefulset') { }}
    <span style="color:#7E5AD4">{{d.resType}}</span>
{{# }  else { }}
    <span style="color:#393D49">{{d.resType}}</span>
{{# } }}
</script>	
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table','laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            laydate = layui.laydate,
            table = layui.table;

        lay('.textboxtime').each(function(){
          laydate.render({
            elem: this,
        	  type: 'datetime',
            format: 'yyyy-MM-dd HH:mm:ss'
          });
        });

        table.render({
            elem: '#currentTableId',
            url: '/xkube/backup/v1/List'+location.search,
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                //{type: "checkbox", width: 50},
                {field: 'id', width:70, title: 'ID', sort: true},
                {field: 'clusterId', title: '集群ID'},
                {field: 'nameSpace', title: '命名空间', sort: true},
                {field: 'resType', title: '类型',templet: '#statusTpl', sort: true},
                {field: 'resName',title: '名称',sort: true},
                {field: 'remarks', title: '备注'},
                {field: 'createtime', width:180,title: '备份时间'},   
                {title: '操作', minWidth: 350, toolbar: '#currentTableBar', align: "center"}           
            ]],
            limits: [25, 50, 100],
            limit: 15,
            page: true
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            data.field.login_user = data.field.login_user.replace(/\s+/g,""); //替换空格
            var result = JSON.stringify(data.field);
            table.reload('currentTableId', {
                //page: {
                //    curr: 1,
                //    limit:50
                //},
                method:'POST',
                url: '/xkube/backup/v1/List',
                contentType: 'application/json',
                where: data.field,
                done: function(res, curr, count){
                }
            }, 'data');

            return false;
        });

        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'createBackup') {  // 监听添加操作
                var index = layer.open({
                    title: '创建备份',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['60%', '90%'],
                    content: '/page/xkube/createBackup.html'+location.search,
                    end: function(){
                    	window.location.reload();//关闭open打开的页面时，刷新父页面
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });
        
         table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'viewYaml') {
                var index = layer.open({
                    title: 'yaml',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['50%', '90%'],
                    content: '/page/xkube/yamlView.html?id='+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }  else if (obj.event === 'restoreBackup') {
      	          layer.confirm('确定恢复该备份?', {icon: 3, title:'提示',yes: function(index){
                       var index2 = layer.load(0, {shade: false});
                       layer.msg('稍等片刻');
                       $.ajax({
                         url: '/xkube/backup/v1/Recover?clusterId='+data.cluster_id+'&nameSpace='+data.name_space+'&resName='+data.res_name+'&resType='+data.res_type,
                         type: "get",
                         dataType: "json",
                         success: function (resp) {
                                  layer.close(index2);
                                   if(resp.code == 0){
                                      layer.msg('重启成功', {icon: 1});
                                   }else{
                                      layer.msg(resp.msg,{icon:2});
                                   }
                          }
                        });		  	  
                    },
            		  cancel: function(index, layero){ 
            			    layer.close(index);
                      layer.close(index2);
            			    console.log("不操作");
            		  } 
        		    });                
            }
        });       
    });
</script>

</body>
</html>